/**
 * Copyright (c) 2015-2016, Javen Zhou  (javenlife@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.jfinal.weixin.demo;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.jfinal.ApiController;
import com.jfinal.weixin.util.JsonResultModel;
import com.jfinal.weixin.util.WeixinUtil;
import com.jfinal.weixin.util.WxJSsign;

public class WeixinJSApiController extends ApiController {
	
	private static Map<String,String> accToken = new HashMap<String,String>();
	
	/**
	 * 如果要支持多公众账号，只需要在此返回各个公众号对应的  ApiConfig 对象即可
	 * 可以通过在请求 url 中挂参数来动态从数据库中获取 ApiConfig 属性值
	 */
	public ApiConfig getApiConfig() {
		return WeixinUtil.getApiConfig();
	}
	
	public void viewWxJS() {
		render("front/publicfun.ftl");
	}
	
	public void signView() {
		String time = accToken.get("time");
		String accessToken = accToken.get("access_token");
		String jsapiTicket = accToken.get("jsapi_ticket");
		Long nowDate = new Date().getTime();
		
		String js_accessToken = "";
		String jsapi_ticket = "";
		
		if (accToken != null && time != null && nowDate - Long.parseLong(time) < 7200 * 1000) {
			js_accessToken = accessToken;
			jsapi_ticket = jsapiTicket;
		} else {
			js_accessToken = WeixinUtil.getJSSDKAccessToken();  //获取微信jssdk---access_token
			jsapi_ticket = WeixinUtil.getJSSDKTicket(js_accessToken); //获取微信jssdk---ticket
	        System.out.println("jsapi_ticket==="+jsapi_ticket);
			accToken.put("time", nowDate + "");
			accToken.put("access_token", js_accessToken);
			accToken.put("jsapi_ticket", jsapi_ticket);
		}
        
        StringBuffer sb = getRequest().getRequestURL();
        //获取完整的URL地址
        String fullPath = sb.toString();
        Map<String,Object> data = WxJSsign.sign(jsapi_ticket, fullPath);
        
        if (data != null && data.get("signature") != null) {
        	data.put("appId", getApiConfig().getAppId());
        	data.put("id", System.currentTimeMillis());
        	setAttr("sign", data);
        	setAttr("url", sb.toString());
        	setAttr("jsapi_ticket", jsapi_ticket);
        	render("front/publicfun.ftl");
        } else {
        	JsonResultModel jrm = new JsonResultModel("300", "验证失败!", data);
            renderJson(jrm);
        }
	}
	
	public void getSignture() {
		getResponse().addHeader("Access-Control-Allow-Origin","*");	
		String time = accToken.get("time");
		String accessToken = accToken.get("access_token");
		String jsapiTicket = accToken.get("jsapi_ticket");
		Long nowDate = new Date().getTime();
		
		String js_accessToken = "";
		String jsapi_ticket = "";
		
		if (accToken != null && time != null && nowDate - Long.parseLong(time) < 10 * 1000) {
			js_accessToken = accessToken;
			jsapi_ticket = jsapiTicket;
		} else {
			js_accessToken = WeixinUtil.getJSSDKAccessToken();  //获取微信jssdk---access_token
			jsapi_ticket = WeixinUtil.getJSSDKTicket(js_accessToken); //获取微信jssdk---ticket
	        System.out.println("jsapi_ticket==="+jsapi_ticket);
			accToken.put("time", nowDate + "");
			accToken.put("access_token", js_accessToken);
			accToken.put("jsapi_ticket", jsapi_ticket);
		}
        
        StringBuffer sb = getRequest().getRequestURL();
        //获取完整的URL地址
        String fullPath = sb.toString();
        fullPath = "http://wxapi.esheyi.com/weixinjs/signView";
//        fullPath = "http://192.168.1.76:9527/getCodeBtn.html";
        Map<String,Object> data = WxJSsign.sign(jsapi_ticket, fullPath);
        
        if (data != null && data.get("signature") != null) {
        	data.put("appId", getApiConfig().getAppId());
        	data.put("id", System.currentTimeMillis());
        	data.put("url", fullPath);
        	JsonResultModel jrm = new JsonResultModel("200", "获取验证成功!", data);
            renderJson(jrm);
        } else {
        	JsonResultModel jrm = new JsonResultModel("300", "获取验证失败!", null);
            renderJson(jrm);
        }
        
	}

}

